Page({
    data: {
        range: {
            material: ['不限'],
            color: ['不限','白','灰','红','蓝','黄','绿','橙','青','紫','粉','棕','荧光'],
            price: ['推荐','低->高','高->低']
        },
        index1: 0,
        index2: 0,
        index3: 0,
        all: [],
        list: [],
        referShow: false
    },
    methods: {
        contains(arr,obj) {
            let i = arr.length 
            while(i--) {
                if(arr[i] === obj) {
                    return true
                }
            }
            return false
        },
        sort(arr,num) {
          for (var i = 0; i < arr.length; i++) {
              for (var j = 0; j < arr.length-1-i; j++) {
                  if (arr[j].price > arr[j+1].price) {
                      let temp = arr[j];
                      arr[j] = arr[j + 1];
                      arr[j + 1] = temp;
                  }
              }
          }
          if(num == 1) {
              return arr;
          }
          else if(num == 2) {
              return arr.reverse()
          }
        }
    },
    onLoad(option) {
        let all = null,list = [],_list = [],list_ = []
        let material = ['不限']
        let color = ['不限']
        let start = () => {
            let promise = new Promise((resolve, reject) => {
                let app = getApp()
                let timer = setInterval(() => {
                    if (app.data.all) {
                        all = app.data.all
                        resolve()
                        clearInterval(timer)
                    }
                }, 1000)
            })
            return promise
        }
        start().then(() => {
            let contains = this.methods.contains
            for(let i=0; i<all.length; i++) {
                if (all[i].style == option.value) {
                    list.push(all[i])
                }
            } // 挑选对应款式
            for(let i=0; i<list.length; i++) {
                list[i]._price = list[i].price
                list[i].price = Number(list[i].price)
                if (isNaN(list[i].price) == true) {
                    list[i].price = Number(list[i]._price.split('-')[0])
                }
                if(!contains(material,list[i].material)) {
                    material.push(list[i].material)
                } // 初始化range.material
                if(list[i].favor == 1) {
                    _list.push(list[i])
                }
                else {
                    list_.push(list[i])
                }
            }
            list = _list.concat(list_) // 推荐置顶
            if(list.length > 0) {
                this.setData({
                    list: list,
                    'range.material': material,
                    // 'range.color': color,
                    all: list
                })
            }
            else {
                this.setData({
                    list: null,
                    all: null
                })
            }
        })
    },
    handleSelect(e) {
        let material = this.data.range.material
        let color = this.data.range.color 
        let length = this.data.range.color.length
        let all = this.data.all
        let list = []
        if(e.currentTarget.dataset.index == 'material') {
            this.setData({
                index1: e.detail.value
            })
        }
        else if(e.currentTarget.dataset.index == 'color') {
            this.setData({
                index2: e.detail.value
            })
        }
        let materialValue = material[this.data.index1]
        let colorValue = color[this.data.index2]
        if(materialValue == '不限' && colorValue == '不限') {
            if(all.length != 0) {
                this.setData({
                    list: all
                })
            }
            else {
                this.setData({
                    list: null
                })
            }        
        }
        else if (materialValue == '不限' && colorValue != '不限'){
            let mark = true
            for (let i in all) {            
                for(let m in all[i].color) {
                    if (all[i].color[m].color2.search(colorValue) != -1) {
                        for(let j in list) {
                            if(list[j].name == all[i].name) {
                                mark = false
                            }
                        }
                        if(mark == true) {
                            list.push(all[i])     
                        } 
                        mark = true            
                    }
                }
            }
            if(list.length != 0) {
                this.setData({
                    list: list
                })
            }
            else {
                this.setData({
                    list: null
                })
            }          
        }
        else if (materialValue != '不限' && colorValue == '不限') {
            for (let i in all) {
                if (all[i].material == materialValue) {
                    list.push(all[i])
                }
            }
            if (list.length != 0) {
                this.setData({
                    list: list
                })
            }
            else {
                this.setData({
                    list: null
                })
            }
        }
        else if (materialValue != '不限' && colorValue != '不限') {
            for (let i in all) {
                for (let m in all[i].color) {
                    if (all[i].color[m].color2.search(colorValue) != -1 && all[i].material == materialValue) {
                        list.push(all[i])
                    }
                }
            }
            if (list.length != 0) {
                this.setData({
                    list: list
                })
            }
            else {
                this.setData({
                    list: null
                })
            }
        }
    },
    handlePrice(e) {
        let list = this.data.list
        let sort = this.methods.sort
        this.setData({
            index3: e.detail.value
        })
        if (e.detail.value == 0) {
            let _list=[],list_=[]
            for(let i in list) {
                if(list[i].favor == 1) {
                    _list.push(list[i])
                }
                else {
                    list_.push(list[i])
                }
            }
            list = _list.concat(list_)
        }
        else if (e.detail.value == 1) {
            list = sort(list,1)
        }
        else if (e.detail.value == 2) {
            list = sort(list,2)
        }
        this.setData({
            list: list
        })
    },
    handleClothes(e) {
        let id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: '/pages/detail/detail?id=' + id,
        })
    },
    handleReferShow() {
        this.setData({
            referShow: true
        })
    },
    handleReferHidden() {
        this.setData({
            referShow: false
        })
    },
})